package controller

import (
	"context"
	"net/http"

	"web/api"
	"web/global"
	"web/proto/myorder"

	"github.com/gin-contrib/sessions"

	"github.com/gin-gonic/gin"
)

type OrderStu struct {
	EndDate   string `json:"end_date"`
	HouseId   string `json:"house_id"`
	StartDate string `json:"start_date"`
}

//下订单
func PostOrders(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	var order OrderStu
	err := ctx.Bind(&order)

	//校验数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "获取数据错误:", err)
		return
	}
	//获取用户名
	userName := sessions.Default(ctx).Get("userName")
	var req myorder.Request = myorder.Request{
		StartDate: order.StartDate,
		EndDate:   order.EndDate,
		HouseId:   order.HouseId,
		UserName:  userName.(string),
	}
	var res myorder.Response
	o := api.NewOrder()
	o.CreateOrder(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data

}

//获取订单信息
func GetUserOrder(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取get请求传参
	role := ctx.Query("role")
	//校验数据
	if role == "" {
		global.GlobalLog.Error(context.Background(), "获取数据失败")
		return
	}
	var req myorder.GetReq = myorder.GetReq{
		Role:     role,
		UserName: sessions.Default(ctx).Get("userName").(string),
	}
	var res myorder.GetResp
	o := api.NewOrder()
	o.GetOrderInfo(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
	resp["data"] = res.Data
}

type StatusStu struct {
	Action string `json:"action"`
	Reason string `json:"reason"`
}

//更新订单状态
func PutOrders(ctx *gin.Context) {
	resp := make(map[string]interface{})
	defer ctx.JSON(http.StatusOK, resp)
	//获取数据
	id := ctx.Param("id")
	var statusStu StatusStu
	err := ctx.Bind(&statusStu)

	//校验数据
	if err != nil || id == "" {
		global.GlobalLog.Error(context.Background(), "获取数据错误:", err)
		return
	}
	var req myorder.UpdateReq = myorder.UpdateReq{
		Action: statusStu.Action,
		Reason: statusStu.Reason,
		Id:     id,
	}
	var res myorder.UpdateResp
	o := api.NewOrder()
	o.UpdateStatus(context.Background(), &req, &res)
	resp["errno"] = res.Errno
	resp["errmsg"] = res.Errmsg
}
